package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.GoodsTransBill;
import com.weixing.mall.provider.model.domain.GoodsTransBillProduct;
import com.weixing.mall.provider.model.dto.GoodsTransBillProductDto;
import com.weixing.mall.provider.model.enums.TransStatusEunm;
import com.weixing.mall.provider.model.query.GoodsTransQuery;
import com.weixing.mall.provider.service.IGoodsTransBillProductService;
import com.weixing.mall.provider.service.IGoodsTransBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName AdminGoodsTransBillProductController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/29
 * @Version 1.0
 **/
@RequestMapping("goodsTransBillProduct")
@RestController
@Api(value = "调货单关联商品Api",tags = "调货单关联商品")
public class GoodsTransBillProductController extends BaseController {

    @Autowired
    private IGoodsTransBillProductService goodsTransBillProductService;

    @Autowired
    private IGoodsTransBillService goodsTransBillService;

    @ApiOperation(value = "根据调拨单id查找关联商品list")
    @GetMapping("getListByGoodsTransId/{goodsTransId}")
    public Result getListByGoodsTransId(@PathVariable Long goodsTransId, GoodsTransQuery goodsTransQuery){

        return ResultUtil.success(goodsTransBillProductService.getGoodsTransBillProductByGoodsTransId(initMpPage(),goodsTransId,goodsTransQuery));
    }
    @ApiOperation(value = "删除调拨单商品")
    @GetMapping("del/{id}")
    public Result del(@PathVariable Long id){
        GoodsTransBillProduct billProduct=goodsTransBillProductService.getById(id);
        GoodsTransBill bill=goodsTransBillService.getById(billProduct.getGoodsTransId());
        if(bill.getTransStatus().equals(TransStatusEunm.TYPE_2.getValue())||bill.getTransStatus().equals(TransStatusEunm.TYPE_3.getValue())){
            return ResultUtil.error("已经确认的调拨单不允许删除");
        }
        bill.setQuantity(bill.getQuantity()-billProduct.getQuantity());
        goodsTransBillService.updateById(bill);
        return ResultUtil.success(goodsTransBillProductService.removeById(id));
    }


    @ApiOperation(value = "修改调拨单商品")
    @PostMapping("update")
    public Result update( @RequestBody  GoodsTransBillProductDto goodsTransBillProductDto){
        GoodsTransBillProduct billProduct=goodsTransBillProductService.getById(goodsTransBillProductDto.getId());
        GoodsTransBill bill=goodsTransBillService.getById(billProduct.getGoodsTransId());
        int num=goodsTransBillProductDto.getQuantity()-billProduct.getQuantity();
        if(bill.getTransStatus().equals(TransStatusEunm.TYPE_2.getValue())||bill.getTransStatus().equals(TransStatusEunm.TYPE_3.getValue())){
            return ResultUtil.error("已经确认的调拨单不允许修改");
        }
        bill.setQuantity(bill.getQuantity()+num);
        goodsTransBillService.updateById(bill);
        GoodsTransBillProduct goodsTransBillProduct=new GoodsTransBillProduct();
        BeanUtils.copyProperties(goodsTransBillProductDto,goodsTransBillProduct);
        return ResultUtil.success(goodsTransBillProductService.updateById(goodsTransBillProduct));
    }
}
